#include "hzpch.h"
#include "Shader.h"
#include "Renderer.h"
#include "Hazel/platform/OpenGL/OpenGLShader.h"
namespace Hazel
{
    Ref<Shader> Shader::Create(const std::string &filepath)
    {
        switch (Renderer::GetAPI())
        {
        case RendererAPI::API::None:
            assert(false);
            return nullptr;
#ifdef HZ_PLATFORM_WINDOWS
        case RendererAPI::API::OpenGL:
            return std::make_shared<OpenGLShader>(filepath);
#endif
        }
        HZ_CORE_ASSERT(false, "Not set Platform in Shader");
        // assert(false);
        return nullptr;
    }

    Ref<Shader> Shader::Create(const std::string &name, const std::string &vertexSrc, const std::string &fragmentSrc)
    {
        switch (Renderer::GetAPI())
        {
        case RendererAPI::API::None:
            assert(false);
            return nullptr;
#ifdef HZ_PLATFORM_WINDOWS
        case RendererAPI::API::OpenGL:
            return std::make_shared<OpenGLShader>(name, vertexSrc, fragmentSrc);
#endif
        }
        HZ_CORE_ASSERT(false, "Not set Platform in Shader");
        // assert(false);
        return nullptr;
    }
    void ShaderLibrary::Add(const std::string &name, const Ref<Shader> &shader)
    {
        HZ_CORE_ASSERT(!Exists(name), "shader already exists!");
        m_Shaders[name] = shader;
    }
    void ShaderLibrary::Add(const Ref<Shader> &shader)
    {
        auto &name = shader->GetName();
        Add(name, shader);
    }
    Ref<Shader> ShaderLibrary::Load(const std::string &filepath)
    {
        auto shader = Shader::Create(filepath);
        Add(shader);
        return shader;
    }
    Ref<Shader> ShaderLibrary::Load(const std::string &name, const std::string &filepath)
    {
        auto shader = Shader::Create(filepath);
        Add(name, shader);
        return shader;
    }
    Ref<Shader> ShaderLibrary::Get(const std::string &name)
    {
        HZ_CORE_ASSERT(Exists(name), "shader not found!");
        return m_Shaders[name];
    }
    bool ShaderLibrary::Exists(const std::string &name) const
    {
        return m_Shaders.find(name) != m_Shaders.end();
    }
}